﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Worker;

namespace InfoJetSoft.Service.Util {
	
	//XmlToEdit工具类.
	class XmlToEditUtil 
	{
		
		//根据名称查找XmlToEdit.
		public static XmlToEdit findXmlToEditByName(Form form, string xmlToEditName) {
			XmlToEdit xmlToEdit = null;
			ArrayList xmlToEditList = form.Manifest.XmlToEditList;
			foreach (XmlToEdit curXmlToEdit in xmlToEditList) {
				if (curXmlToEdit.Name.Equals(xmlToEditName)) {
					xmlToEdit = curXmlToEdit;
					break;
				}
			}
            //CID 0708 //2010.11.19 //Comment
            /*
			if (xmlToEdit == null) {
				throw new InfoJetException("no xmlToEdit " + xmlToEditName, "EU01");
			}
             */
            //CID 0708 //2010.11.19 //Comment
			return xmlToEdit;
		}

		//根据名称查找在指定View中的XmlToEdit.
		public static XmlToEdit findXmlToEditInView(Form form, string xmlToEditName, string viewName) 
		{
			XmlToEdit xmlToEdit = null;
			ArrayList xmlToEditList = form.Manifest.XmlToEditList;
			foreach (XmlToEdit curXmlToEdit in xmlToEditList) 
			{
				if (curXmlToEdit.Name.Equals(xmlToEditName) && curXmlToEdit.ViewName.Equals(viewName)) 
				{
					xmlToEdit = curXmlToEdit;
					break;
				}
			}
			return xmlToEdit;
		}
		
		public static XmlNode findNodeByContextId(XmlNodeList nodeList, ArrayList contextIdList) {
			ArrayList containerIdList = new ArrayList();
			foreach (XmlNode node in nodeList) {
				containerIdList.Add(DOMUtil.getNodeJetId(node));
			}
			foreach (string contextId in contextIdList) {
				for (int c = 0; c < containerIdList.Count; c++) {
					string containerId = (string) containerIdList[ c ];
					if (contextId.Equals(containerId)) {
						return nodeList[ c ];
					}
				}
			}
			return null;
		}

		public static string buildContextIdListByNode( Form form, XmlNode node )
		{
			string contextIds = DOMUtil.getNodeJetId( node );
			//CID 0210 //2009.01.20 //Begin
			//有传入DocumentElement的情况.
			if( node.Equals( form.XmlDoc.DocumentElement ) )
			{
				return contextIds;
			}
			//CID 0210 //2009.01.20 //End

			XmlNode parentNode = null;
			if( node is XmlAttribute )
			{
				parentNode = ((XmlAttribute)node).OwnerElement;
			}
			else
			{
				parentNode = node.ParentNode;
			}

			while( !parentNode.Equals( form.XmlDoc.DocumentElement ) )
			{
				contextIds = contextIds + JetConstant.ID_SEPARATOR + DOMUtil.getNodeJetId( parentNode );
				parentNode = parentNode.ParentNode;
			}

			contextIds = contextIds + JetConstant.ID_SEPARATOR + DOMUtil.getNodeJetId( form.XmlDoc.DocumentElement );
			return contextIds;
		}

		public static ArrayList getItemComponents( Form form, XmlNode node )
		{
			ArrayList compList = new ArrayList();
			string path = DOMUtil.getNodePath( node );
			foreach( XmlToEdit xmlToEdit in form.Manifest.XmlToEditList )
			{
				if( xmlToEdit.Item.Equals( path ) )
				{
					compList.Add( xmlToEdit.Component );
				}
			}
			return compList;
		}

		public static void domEventHandler( Form form, XmlToEditContext context, XmlElement insertedElement )
		{ 
			if( insertedElement == null || form.Manifest.DomEventHandlerList == null )
			{
				return;
			}
			foreach( DOMEventHandler handler in form.Manifest.DomEventHandlerList )
			{
				if( context.XmlToEdit.Item.Equals( handler.Match ) )
				{
					FormRuleSectionWorker.runRule( form, handler, insertedElement );
				}
			}
		}

		//CID 0216 //2009.01.21 //Begin
		public static string buildContextIdArray( Form form )
		{
			string contextIdArray = null;
			if( form.SelectNode == null )
			{
				//如果没有SelectNode,使用文档元素作为上下文
				contextIdArray = DOMUtil.getNodeJetId( form.XmlDoc.DocumentElement );
			}
			else
			{
				if( form.ContextNodeList != null )
				{
					//先检查ConextNodeList中的结点未被删除,从上面开始查找.
					ArrayList reversedContextNodes = new ArrayList();
					reversedContextNodes.AddRange(form.ContextNodeList);
					reversedContextNodes.Reverse();
					//默认设置为DocumentElement.
					XmlNode firstContextNode = form.XmlDoc.DocumentElement;
					//判断Context中的Node是连续的.
					bool validContext = true;
					foreach( XmlNode contextNode in reversedContextNodes )
					{
						if( !DOMUtil.hasOwnerNode( contextNode ) )
						{
							validContext = false;
							break;
						}
						firstContextNode = contextNode;
					}
					if( validContext )
					{
						//使用SelectNode构造ContextIdArray.
						contextIdArray = XmlToEditUtil.buildContextIdListByNode( form, form.SelectNode );
					}
					else
					{
						contextIdArray = XmlToEditUtil.buildContextIdListByNode( form, firstContextNode );
					}
				}
				else
				{
					contextIdArray = XmlToEditUtil.buildContextIdListByNode( form, form.SelectNode );
				}
			}
			return contextIdArray;
		}
		//CID 0216 //2009.01.21 //End

		//CID 0216 //2009.01.21 //Begin
		public static void refreshContextNodeListBySelectNode( Form form )
		{
			if( form.ContextNodeList == null )
			{
				form.ContextNodeList = new ArrayList();
			}
			form.ContextNodeList.Clear();

			bool validSelectedNode = true;
			XmlNode selectedNode = form.SelectNode;
			if( selectedNode.Equals( form.XmlDoc.DocumentElement ) )
			{
				form.ContextNodeList.Add( selectedNode );
			}
			else
			{
				form.ContextNodeList.Add( selectedNode );
				selectedNode = DOMUtil.getOwnerNode( selectedNode );
				while( selectedNode != null && !(selectedNode is XmlDocument) )
				{
					form.ContextNodeList.Add( selectedNode );
					selectedNode = DOMUtil.getOwnerNode( selectedNode );
				}
				//未达到DocumentElement就中断了,说明给定的SelectNode已经被删除.
				if( !form.ContextNodeList[ form.ContextNodeList.Count - 1 ].Equals( form.XmlDoc.DocumentElement ) )
				{
					validSelectedNode = false;
				}
			}
			if( !validSelectedNode )
			{
				//如果无效,自动设置为DocumentElement.
				form.ContextNodeList.Add( form.XmlDoc.DocumentElement );
			}
		}
		//CID 0216 //2009.01.21 //End

	}
}